/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.deptBudget.bean;

import com.ibatis.dao.client.DaoManager;
import com.yunhoo.deptBudget.db.dao.FinBudgetDao;
import com.yunhoo.deptBudget.model.Budget;
import com.yunhoo.sys.db.config.DaoConfig;
import com.yunhoo.sys.util.common.Null;
import com.yunhoo.sys.util.common.CommonParam;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author Administrator
 */
@ManagedBean(name = "finBudgetEditBean")
@SessionScoped
public class FinBudgetEditBean implements Serializable {

    private static DaoManager daom;
    private static FinBudgetDao dao;
    //
    private List<Budget> editli = null;
    private boolean editStatus = false;
    //
    private List<SelectItem> yearli = null;
    private Integer iyear = 0;

    public FinBudgetEditBean() {
        daom = DaoConfig.getDaoManager();
        dao = (FinBudgetDao) daom.getDao(FinBudgetDao.class);
    }

    public void queryli() {
        initli();
    }

    private void initli() {
        try {
            Budget param = new Budget(true);
            param.setIyear(iyear);
            editli = dao.queryListByOrgAndYear(param);
        } catch (SQLException ex) {
            Logger.getLogger(FinBudgetEditBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void edit() {
        editStatus = true;
    }

    public void checkSave() {
        boolean ok = save();
        if (ok) {
            editStatus = false;
        }
    }

    public boolean save() {
        if (editli == null || editli.isEmpty()) {
            return false;
        }
        boolean ok = true;
        List<Budget> templi = new ArrayList<Budget>();
        for (Budget budget : editli) {
            if (!Null.check(budget.getBudget())) {//预算数据 不为null 不为0
                budget.setIyear(iyear);
                templi.add(budget);
            }
        }
        try {
            daom.startTransaction();
            dao.deleteByOrgAndYear(templi.get(0));
            dao.insertList(templi);
            daom.commitTransaction();
        } catch (SQLException ex) {
            ok = false;
            daom.endTransaction();
            Logger.getLogger(FinBudgetEditBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ok;
    }

    public void cancel() {
        initli();
        editStatus = false;
    }

    public List<Budget> getEditli() {
        return editli;
    }

    public void setEditli(List<Budget> editli) {
        this.editli = editli;
    }

    public boolean isEditStatus() {
        return editStatus;
    }

    public void setEditStatus(boolean editStatus) {
        this.editStatus = editStatus;
    }

    public List<SelectItem> getYearli() {
        if (yearli == null) {
            yearli = CommonParam.yearList();
        }
        return yearli;
    }

    public void setYearli(List<SelectItem> yearli) {
        this.yearli = yearli;
    }

    public Integer getIyear() {
        return iyear;
    }

    public void setIyear(Integer iyear) {
        this.iyear = iyear;
    }
}
